package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ReceptionMessageAdapter;
import com.wnhz.work.yz.bean.ReceptionMessageBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---接待信息
 */
public class ReceptionMessageActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ListView my_listView;
    private ReceptionMessageAdapter mAdapter;
    private List<ReceptionMessageBean.InfoEntity> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reception_message);
    }

    @Override
    protected void onResume() {
        upLoad();
        super.onResume();
    }

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
        XUtil.Post(Url.INFORMATION_ADMITLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");

                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        ReceptionMessageBean b = gson.fromJson(result, ReceptionMessageBean.class);
                        mList = b.getInfo();
                        initData();
                        initView();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void initData() {
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("接待信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        my_listView = (ListView) findViewById(R.id.my_listView);
        mAdapter = new ReceptionMessageAdapter(this, this, mList);
        my_listView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        int position;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //安排行程
            case R.id.tv_arrange:
                position = (int) v.getTag();
                intent = new Intent(this, ArrangeRouteActivity.class);
                intent.putExtra("info_id", mList.get(position).getInfo_id());
                startActivity(intent);
                break;

            //转交他人
            case R.id.tv_pass:
                position = (int) v.getTag();
                intent = new Intent(this, PassOthersActivity.class);
                startActivity(intent);
                break;

            //查看行程
            case R.id.tv_look:
                position = (int) v.getTag();
                intent = new Intent(this, ArrangeRouteActivity.class);
                intent.putExtra("arrange_id", mList.get(position).getArrange_id());
                intent.putExtra("info_id", mList.get(position).getInfo_id());
                startActivity(intent);
                break;

            //反馈
            case R.id.tv_feedback:
                position = (int) v.getTag();
                intent = new Intent(this, FeedbackActivity.class);
                intent.putExtra("info_id", mList.get(position).getInfo_id());
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
